package com.sskj.givecoin.p.fragment;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.givecoin.m.HttpConfig;
import com.sskj.givecoin.m.bean.HttpData;
import com.sskj.givecoin.m.http.JsonCallBack;
import com.sskj.givecoin.p.activity.RegisterPresenter;
import com.sskj.givecoin.p.base.BasePresenter;
import com.sskj.givecoin.utils.MD5Util;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.fragment.RegisterFragment;

public class RegisterFragmentPresenter extends BasePresenter<RegisterFragment> {
    public void sendCode(String account) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CHECK_PHONE_EXIST)
                .params("mobile", account)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SMS_SEND)
                                    .params("mobile", account)
                                    .params("type", 1)
                                    .execute(new JsonCallBack<HttpData>(RegisterFragmentPresenter.this) {
                                        @Override
                                        public void _onSuccess(Response<HttpData> response) {
                                            HttpData httpListData = response.body();
                                            if (httpListData.getStatus() == 200) {
                                                mView.sendCodeOk(account);
                                            } else {
                                                ToastUtil.showShort(httpListData.getMsg());
                                            }
                                        }

                                        @Override
                                        public void onError(Response<HttpData> response) {
                                            super.onError(response);
                                        }
                                    });
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }

    public void register(String account, String password, String password1, String tjuser, String code) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.REGISTER)
                .params("mobile", account)
                .params("opwd", MD5Util.md5FiveTime(password))
                .params("opwd1", MD5Util.md5FiveTime(password1))
                .params("tjuser", tjuser)
                .params("code", code)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        mView.registerSuccess();
                    }
                });
    }

    public void sendEmail(String email) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SEND_EMAIL)
                .params("email", email)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.sendCodeOk(email);
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }
}
